package com.setting.defpiechartutils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;

import com.github.mikephil.charting.R;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
import com.github.mikephil.charting.databinding.ListPieChartBinding;
import com.setting.base.BaseBindingAdapter;
import com.setting.model.Datapiechart;
import com.setting.view.DefPieChart;

import java.util.List;

public class DefPieChartSetting {

    private String pink = "#FF69B4";
    private String blue = "#108ee9";
    private String red = "#ef0000";
    private String green = "#6db81f";
    private String purple = "#FF6200EE";
    private String gray = "#9a9a9a";
    private String black = "#00002c";

    private Context context;
    private DefPieChart defPieChart;
    //饼状图数据集
    private PieDataSet set;
    private Legend legend;
    private BaseBindingAdapter<Datapiechart, ListPieChartBinding> nAdapter;

    public DefPieChartSetting(){

    }

    public DefPieChartSetting(Context context, DefPieChart defPieChart) {
        this.context = context;
        this.defPieChart = defPieChart;

        if (nAdapter == null) {
            nAdapter = new BaseBindingAdapter<Datapiechart, ListPieChartBinding>(getContext()) {
                @Override
                protected int getLayoutResId(int viewType) {
                    return R.layout.list_pie_chart;
                }

                @SuppressLint("ResourceAsColor")
                @Override
                protected void onBindItem(ListPieChartBinding binding, Datapiechart item) {
                    binding.setData(item);
                    binding.rlColor.setBackgroundColor(item.getLablecolor());
                    binding.tvNum.setTextColor(item.getLablecolor());
                    binding.tvPercentNum.setTextColor(item.getLablecolor());
                    binding.tvPercent.setTextColor(item.getLablecolor());

                }
            };
        }
    }

    public Context getContext() {
        return context;
    }

    public void setContext(Context context) {
        this.context = context;
    }

    public DefPieChart getDefPieChart() {
        return defPieChart;
    }

    public void setDefPieChart(DefPieChart defPieChart) {
        this.defPieChart = defPieChart;
    }

    public PieDataSet getSet() {
        return set;
    }

    public void setSet(PieDataSet set) {
        this.set = set;
    }

    public Legend getLegend() {
        return legend;
    }

    public void setLegend(Legend legend) {
        this.legend = legend;
    }

    public BaseBindingAdapter<Datapiechart, ListPieChartBinding> getnAdapter() {
        return nAdapter;
    }

    public void setnAdapter(BaseBindingAdapter<Datapiechart, ListPieChartBinding> nAdapter) {
        this.nAdapter = nAdapter;
    }

    public static class SetAttribute{
        private DefPieChartSetting setting;
        private boolean drawvalues = true;
        private float valuetextsize = 17f;
        private int valuetextcolor = Color.BLACK;
        private Typeface valuetypeface;
        private PieDataSet.ValuePosition yvalueposition = PieDataSet.ValuePosition.INSIDE_SLICE;
        private float valuelinepart1length = 0.3f;
        private float valuelinepart2length = 0.4f;
        private float valuelinepart1Offsetpercentage = 75.f;
        private boolean descriptionenabled = true;
        private String descriptiontext = "Description Label";
        private int backgroundcolor;
        private float extraoffsetsleft = 0.f;
        private float extraoffsetstop = 0.f;
        private float extraoffsetsright = 0.f;
        private float extraoffsetsbottom = 0.f;
        private boolean drawentrylabels = true;
        private boolean rotationenabled = true;
        private boolean usepercentvalues = false;
        private boolean drawholeenabled = true;
        private boolean highlightpertapenabled = true;
        private float holeradius = 50f;
        private int holecolor = Color.WHITE;
        private boolean drawcentertext = true;
        private String centertext = "";
        private String centercontent = "";
        private float centertextsize = 12f;
        private float centercontentsize = 12f;
        private int centertextcolor = Color.BLACK;
        private int centercontentcolor = Color.BLACK;

        private boolean enabled = false;
        private Legend.LegendVerticalAlignment verticalalignment = Legend.LegendVerticalAlignment.BOTTOM;
        private Legend.LegendHorizontalAlignment horizontalalignment = Legend.LegendHorizontalAlignment.LEFT;
        private Legend.LegendOrientation orientation = Legend.LegendOrientation.HORIZONTAL;
        private Legend.LegendForm form = Legend.LegendForm.SQUARE;
        private float formsize = 8f;
        private float formtotextspace = 5f;
        private boolean drawinside = false;
        private boolean wordwrapenabled = false;
        private float xentryspace = 6f;
        private float yentryspace = 0f;
        private float yoffset = 3f;
        private float textsize = 10f;
        private int textcolor = Color.BLACK;
        private Typeface centertexttype = Typeface.DEFAULT;
        private Typeface centercontenttype = Typeface.DEFAULT;

        public DefPieChartSetting.SetAttribute setting(DefPieChartSetting setting) {
            this.setting = setting;
            return this;
        }
        public DefPieChartSetting.SetAttribute drawvalues(boolean drawvalues) {
            this.drawvalues = drawvalues;
            return this;
        }
        public DefPieChartSetting.SetAttribute valuetextsize(float valuetextsize) {
            this.valuetextsize = valuetextsize;
            return this;
        }
        public DefPieChartSetting.SetAttribute valuetextcolor(int valuetextcolor) {
            this.valuetextcolor = valuetextcolor;
            return this;
        }
        public DefPieChartSetting.SetAttribute valuetypeface(Typeface valuetypeface) {
            this.valuetypeface = valuetypeface;
            return this;
        }
        public DefPieChartSetting.SetAttribute yvalueposition(PieDataSet.ValuePosition yvalueposition) {
            this.yvalueposition = yvalueposition;
            return this;
        }
        public DefPieChartSetting.SetAttribute valuelinepart1length(float valuelinepart1length) {
            this.valuelinepart1length = valuelinepart1length;
            return this;
        }
        public DefPieChartSetting.SetAttribute valuelinepart2length(float valuelinepart2length) {
            this.valuelinepart2length = valuelinepart2length;
            return this;
        }
        public DefPieChartSetting.SetAttribute valuelinepart1Offsetpercentage(float valuelinepart1Offsetpercentage) {
            this.valuelinepart1Offsetpercentage = valuelinepart1Offsetpercentage;
            return this;
        }
        public DefPieChartSetting.SetAttribute descriptionenabled(boolean descriptionenabled) {
            this.descriptionenabled = descriptionenabled;
            return this;
        }
        public DefPieChartSetting.SetAttribute descriptiontext(String descriptiontext) {
            this.descriptiontext = descriptiontext;
            return this;
        }
        public DefPieChartSetting.SetAttribute backgroundcolor(int backgroundcolor) {
            this.backgroundcolor = backgroundcolor;
            return this;
        }
        public DefPieChartSetting.SetAttribute extraoffsets(float extraoffsetsleft,float extraoffsetstop,float extraoffsetsright,float extraoffsetsbottom) {
            this.extraoffsetsleft = extraoffsetsleft;
            this.extraoffsetstop = extraoffsetstop;
            this.extraoffsetsright = extraoffsetsright;
            this.extraoffsetsbottom = extraoffsetsbottom;
            return this;
        }
        public DefPieChartSetting.SetAttribute drawentrylabels(boolean drawentrylabels) {
            this.drawentrylabels = drawentrylabels;
            return this;
        }
        public DefPieChartSetting.SetAttribute rotationenabled(boolean rotationenabled) {
            this.rotationenabled = rotationenabled;
            return this;
        }
        public DefPieChartSetting.SetAttribute usepercentvalues(boolean usepercentvalues) {
            this.usepercentvalues = usepercentvalues;
            return this;
        }
        public DefPieChartSetting.SetAttribute drawholeenabled(boolean drawholeenabled) {
            this.drawholeenabled = drawholeenabled;
            return this;
        }
        public DefPieChartSetting.SetAttribute highlightpertapenabled(boolean highlightpertapenabled) {
            this.highlightpertapenabled = highlightpertapenabled;
            return this;
        }
        public DefPieChartSetting.SetAttribute holeradius(float holeradius) {
            this.holeradius = holeradius;
            return this;
        }
        public DefPieChartSetting.SetAttribute holecolor(int holecolor) {
            this.holecolor = holecolor;
            return this;
        }
        public DefPieChartSetting.SetAttribute drawcentertext(boolean drawcentertext) {
            this.drawcentertext = drawcentertext;
            return this;
        }
        public DefPieChartSetting.SetAttribute centertext(String centertext,String centercontent) {
            this.centertext = centertext;
            this.centercontent = centercontent;
            return this;
        }
        public DefPieChartSetting.SetAttribute centertextsize(float centertextsize,float centercontentsize) {
            this.centertextsize = centertextsize;
            this.centercontentsize = centercontentsize;
            return this;
        }
        public DefPieChartSetting.SetAttribute centertextcolor(int centertextcolor,int centercontentcolor) {
            this.centertextcolor = centertextcolor;
            this.centercontentcolor = centercontentcolor;
            return this;
        }

        public DefPieChartSetting.SetAttribute centertexttypeface(Typeface centertexttype,Typeface centercontenttype) {
            this.centertexttype = centertexttype;
            this.centercontenttype = centercontenttype;
            return this;
        }

        public DefPieChartSetting.SetAttribute enabled(boolean enabled) {
            this.enabled = enabled;
            return this;
        }
        public DefPieChartSetting.SetAttribute verticalalignment(Legend.LegendVerticalAlignment verticalalignment) {
            this.verticalalignment = verticalalignment;
            return this;
        }
        public DefPieChartSetting.SetAttribute horizontalalignment(Legend.LegendHorizontalAlignment horizontalalignment) {
            this.horizontalalignment = horizontalalignment;
            return this;
        }
        public DefPieChartSetting.SetAttribute orientation(Legend.LegendOrientation orientation) {
            this.orientation = orientation;
            return this;
        }
        public DefPieChartSetting.SetAttribute form(Legend.LegendForm form) {
            this.form = form;
            return this;
        }
        public DefPieChartSetting.SetAttribute formsize(float formsize) {
            this.formsize = formsize;
            return this;
        }
        public DefPieChartSetting.SetAttribute formtotextspace(float formtotextspace) {
            this.formtotextspace = formtotextspace;
            return this;
        }
        public DefPieChartSetting.SetAttribute drawinside(boolean drawinside) {
            this.drawinside = drawinside;
            return this;
        }
        public DefPieChartSetting.SetAttribute wordwrapenabled(boolean wordwrapenabled) {
            this.wordwrapenabled = wordwrapenabled;
            return this;
        }
        public DefPieChartSetting.SetAttribute xentryspace(float xentryspace) {
            this.xentryspace = xentryspace;
            return this;
        }
        public DefPieChartSetting.SetAttribute yentryspace(float yentryspace) {
            this.yentryspace = yentryspace;
            return this;
        }
        public DefPieChartSetting.SetAttribute yoffset(float yoffset) {
            this.yoffset = yoffset;
            return this;
        }
        public DefPieChartSetting.SetAttribute textsize(float textsize) {
            this.textsize = textsize;
            return this;
        }
        public DefPieChartSetting.SetAttribute textcolor(int textcolor) {
            this.textcolor = textcolor;
            return this;
        }

        public void setData(List<PieEntry> list,List<Integer> colors){
            setting.set = new PieDataSet(list,"");
            setting.set.setColors(colors);
            //设置是否显示文本
            setting.set.setDrawValues(this.drawvalues);
            //文字的大小
            setting.set.setValueTextSize(this.valuetextsize);
            //文字的颜色
            setting.set.setValueTextColor(this.valuetextcolor);
            //文字的样式
            setting.set.setValueTypeface(this.valuetypeface);
            //设置Y值的位置是在圆内还是圆外
            setting.set.setYValuePosition(this.yvalueposition);
            //当值位置为外边线时，表示线的前半段长度。
            setting.set.setValueLinePart1Length(this.valuelinepart1length);
            //当值位置为外边线时，表示线的后半段长度。
            setting.set.setValueLinePart2Length(this.valuelinepart2length);
            //当ValuePosition为OUTSIDE_SLICE时，指示偏移为切片大小的百分比
            setting.set.setValueLinePart1OffsetPercentage(this.valuelinepart1Offsetpercentage);
            PieData pieData = new PieData(setting.set);
            setting.defPieChart.setData(pieData);
            //将图表重绘以显示设置的属性和数据
            setting.defPieChart.invalidate();
            //隐藏描述
            setting.defPieChart.getDescription().setEnabled(this.descriptionenabled);
            //设置pieChart图表的描述
            setting.defPieChart.getDescription().setText(this.descriptiontext);
            //设置pieChart图表背景色
            setting.defPieChart.setBackgroundColor(this.backgroundcolor);
            //设置pieChart图表上下左右的偏移，类似于外边距
            setting.defPieChart.setExtraOffsets(this.extraoffsetsleft,this.extraoffsetstop,this.extraoffsetsright,this.extraoffsetsbottom);
            //设置pieChart是否只显示饼图上百分比不显示文字
            setting.defPieChart.setDrawEntryLabels(this.drawentrylabels);
            //设置pieChart图表是否可以手动旋转
            setting.defPieChart.setRotationEnabled(this.rotationenabled);
            //使用百分比显示
            setting.defPieChart.setUsePercentValues(this.usepercentvalues);
            //是否显示PieChart内部圆环
            setting.defPieChart.setDrawHoleEnabled(this.drawholeenabled);
            //设置piecahrt图表点击Item高亮是否可用
            setting.defPieChart.setHighlightPerTapEnabled(this.highlightpertapenabled);
            //设置PieChart内部圆的半径
            setting.defPieChart.setHoleRadius(this.holeradius);
            //设置PieChart内部圆的颜色
            setting.defPieChart.setHoleColor(this.holecolor);
            //是否绘制PieChart内部中心文本
            setting.defPieChart.setDrawCenterText(this.drawcentertext);
            //设置PieChart内部圆文字的内容
            setting.defPieChart.setCenterText(this.centertext,this.centercontent);
            //设置PieChart内部圆文字的大小
            setting.defPieChart.setCenterTextSize(this.centertextsize,this.centercontentsize);
            //设置PieChart内部圆文字的颜色
            setting.defPieChart.setCenterTextColor(this.centertextcolor,this.centercontentcolor);
            //设置PieChart内部圆文字的样式
            setting.defPieChart.setCenterTextTypeface(this.centertexttype,this.centercontenttype);

            setting.legend = setting.defPieChart.getLegend();
            //是否启用图列（true：下面属性才有意义）
            setting.legend.setEnabled(this.enabled);
            //图例位置
            setting.legend.setVerticalAlignment(this.verticalalignment);
            setting.legend.setHorizontalAlignment(this.horizontalalignment);
            setting.legend.setOrientation(this.orientation);
            //设置图例的形状
            setting.legend.setForm(this.form);
            //设置图例的大小
            setting.legend.setFormSize(this.formsize);
            //设置每个图例实体中标签和形状之间的间距
            setting.legend.setFormToTextSpace(this.formtotextspace);
            //设置是否画在图表里
            setting.legend.setDrawInside(this.drawinside);
            //设置图列换行(注意使用影响性能,仅适用legend位于图表下面)
            setting.legend.setWordWrapEnabled(this.wordwrapenabled);
            //设置图例实体之间延X轴的间距（setOrientation = HORIZONTAL有效）
            setting.legend.setXEntrySpace(this.xentryspace);
            //设置图例实体之间延Y轴的间距（setOrientation = VERTICAL 有效）
            setting.legend.setYEntrySpace(this.yentryspace);
            //设置比例块Y轴偏移量
            setting.legend.setYOffset(this.yoffset);
            //设置图例标签文本的大小
            setting.legend.setTextSize(this.textsize);
            //设置图例标签文本的颜色
            setting.legend.setTextColor(this.textcolor);

        }
    }

}
